const puppeteer = require('puppeteer');
const url = `https://movie.douban.com/tag/#/?sort=S&range=6,10`;

// promise版本的定时函数
const sleep = time => new Promise((resolve, reject) => {
    setTimeout(resolve, time);
});

;(async () => {
    console.log('start visit the target page');
    const browser = await puppeteer.launch({
        args: ['--no-sandbox'],
        dumpio: false
    })
    const page = await browser.newPage();
    await page.goto(url, {
        waitUntil: 'networkidle2'
    });

    // 延时等待3秒,是为了下一步,页面等待选择器元素的出现,如果出现了,立即返回
    await sleep(3000);
    // 等待页面出现了"加载更多按钮", 并选择
    await page.waitForSelector('.more');

    // 模拟多次点击,此处可以设置i < 10等
    for (let i = 0; i < 1; i++) {
        await sleep(3000);
        await page.click('.more');
    }
    // 加载了很多页之后,就可以获取加载的网页内容了,
    // evaluate中的回调函数是在浏览器中执行的代码
    const result = await page.evaluate( () => {
        let $ = window.$;               // 因为是在浏览器执行, 
        let items = $('.list-wp a');
        let links = [];
        if (items.length >= 1) {
            items.each( (index, item) => {
                let it = $(item);
                let doubanId = it.find('div').data('id');
                let title = it.find('.title').text();
                let rate = it.find('.rate').text();
                let poster = it.find('img').attr('src').replace('s_ratio', 'l_ratio');
                links.push({
                    doubanId, title, rate, poster
                })
            })
        }
        return links;   // 返回信息数组
    })
    browser.close();
    console.log(result);
})()